package com.xyz.yygh.common.result;

import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: husngSir
 * @Date: 2021/04/16/9:32
 * @Description:
 */

public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    // 默认是成功态
    public R() {
        put("code", ResultCodeEnum.SUCCESS.getCode());
        put("message",  ResultCodeEnum.SUCCESS.getMessage());
    }

    public static R error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("message", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("message", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        R r = new R();
        return r;
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public <T> R putData(T data) {
        super.put("data", data);
        return this;
    }

    public R putPage(Long total, Long current, Long pageSize) {
        super.put("total", total);
        super.put("current", current);
        super.put("pageSize", pageSize);
        return this;
    }
    
}
